package com.fintech.third.service.message;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dragon.common.entity.search.Searchable;
import com.dragon.common.repository.mybatis.paginator.domain.Order;
import com.dragon.common.repository.mybatis.paginator.domain.PageBounds;
import com.dragon.common.repository.mybatis.paginator.domain.PageList;
import com.dragon.common.utils.PageListJsonUtils;
import com.fintech.third.dao.message.AcPushMessageMapper;
import com.fintech.third.model.message.AcPushMessage;

@Service("acPushMessageService")
public class AcPushMessageServiceImpl implements AcPushMessageService {
	
	@Autowired
	private AcPushMessageMapper acPushMessageMapper;
	
	@Override
	public void insert(AcPushMessage AcPushMessage) {
		acPushMessageMapper.insertSelective(AcPushMessage);
	}

	@Override
	public void update(AcPushMessage AcPushMessage) {
		acPushMessageMapper.updateByPrimaryKeySelective(AcPushMessage);
	}

	@Override
	public AcPushMessage getById(Integer id) {
		return acPushMessageMapper.selectByPrimaryKey(id);
	}

	@Override
	public void delete(Integer id) {
		acPushMessageMapper.deleteByPrimaryKey(id);
	}

	@Override
	public Map<String, Object> listAcPushMessage(Integer pageIndex, Integer pageSize, String userId) {
		if(pageIndex <= 0) pageIndex = 1;
		List<Map<String,Object>> resultList = null;
		
		// 封装过滤条件
		Searchable searchable = Searchable.newSearchable();
		searchable.addSearchParam("deleteFlag_eq", 0);
		searchable.addSearchParam("userId_eq", userId);
		searchable.addSearchParam("pushStatus_eq", "1");
		PageBounds pageBounds = new PageBounds(pageIndex,pageSize);
		List<Order> orders = new ArrayList<>();
		orders.add(Order.create("id", "desc"));
		pageBounds.setOrders(orders);
		resultList = acPushMessageMapper.listAcPushMessage(searchable, pageBounds);
		
		PageList<Map<String, Object>> pageList = (PageList<Map<String, Object>>)resultList;
		Map<String,Object> resultMap = new HashMap<String,Object>();
		PageListJsonUtils.serialize(pageList, resultList, resultMap);
		return resultMap;
	}

	@Override
	public Integer unreadCountByUserId(String userId) {
		return acPushMessageMapper.unreadCountByUserId(userId);
	}

	@Override
	public void setReadByUserId(String id, String userId) {
		acPushMessageMapper.setReadByUserId(id, userId);
	}

	@Override
	public Map<String, Object> selectSMSSendByMobile(String mobile) {
		return acPushMessageMapper.selectSMSSendByMobile(mobile);
	}

	@Override
	public Integer selectCount(String mobile) {
		return acPushMessageMapper.selectCount(mobile);
	}

	
}
